//
//  HomeDiyDiyViewController.m
//  Mahjong
//
//  Created by KING on 16/9/12.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "HomeDiyDiyViewController.h"
#import "Header.h"
#import "Second1TableViewCell.h"
#import "LIstDiyEditTableViewCell.h"
#import "ListDiyDEditTableViewCell.h"
#import "CustomViewController.h"
#import "OrderWuLiaoViewController.h"
#import "AddCustomViewController.h"
@interface HomeDiyDiyViewController ()<UITableViewDelegate,UITableViewDataSource,editDelegate>
@property (nonatomic, strong) UITableView *diyTableView;
@property (nonatomic, strong) NSMutableArray *numArray;
@property (nonatomic, strong) UITextField *field;
@end

@implementation HomeDiyDiyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSArray *arr = @[@"12",@"13"];
    self.numArray = [NSMutableArray arrayWithArray:arr];
    [self createDiyTableView];
}
-(void)createDiyTableView{
    
    self.diyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    self.diyTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];

    [self.view addSubview:self.diyTableView];
    self.diyTableView.delegate = self;
    self.diyTableView.dataSource = self;
    [self.diyTableView registerNib:[UINib  nibWithNibName:@"Second1TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.diyTableView registerNib:[UINib nibWithNibName:@"LIstDiyEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"editCell"];
    [self.diyTableView registerNib:[UINib nibWithNibName:@"ListDiyDEditTableViewCell" bundle:nil] forCellReuseIdentifier:@"deditCell"];
    [self createView];
    self.diyTableView.tableFooterView = [[UIView alloc]init];
    
}
-(void)createView{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENH_HEIGHT-210, SCREEN_WIDTH, 100)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
    la.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];

    la.text = @"发布订单后";
    la.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:la];
    
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 25)];
    lb.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];

    lb.text = @"我们将在12小时内与你联系";
    lb.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:lb];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 50, SCREEN_WIDTH, 50);
    [button setTitle:@"发布定制需求" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor redColor];
    [backView addSubview:button];
    [button addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)sendAction:(UIButton *)btn{
    OrderWuLiaoViewController *ord = [[OrderWuLiaoViewController alloc] init];
    ord.mian = self.field.text;
    UINavigationController *naVC  = [[UINavigationController alloc] initWithRootViewController:ord];
    [self presentViewController:naVC animated:YES completion:nil];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        Second1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        self.field = [[UITextField alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-120, 10, 70, 20)];
        self.field.placeholder = @"输入面积";
        [self.field setFont:[UIFont systemFontOfSize:16]];
        [cell.contentView addSubview:self.field];
//        [cell.fixBtn addTarget:self action:@selector(handelFixAction:) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }
    else{
         ListDiyDEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deditCell"];
        cell.number = 45;
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
            cell.addButton.alpha = 0;
            cell.deleButton.alpha = 0;
            cell.remoButton.alpha = 0;
            cell.numLabel.font = [UIFont systemFontOfSize:18];
//            LIstDiyEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"editCell"];
//            cell.numLabel.text = self.numArray[indexPath.row];
//            return cell;
        }
        else{
//            ListDiyDEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deditCell"];
//            [cell nsarray:self.numArray success:^(id ar) {
//                
//                self.numArray = ar;
//            }];
//            NSLog(@"-%@-",self.numArray);
//            cell.numArray = self.numArray;
//            cell.myDelegate = self;
            cell.addButton.alpha = 1;
            cell.deleButton.alpha = 1;
            cell.remoButton.alpha = 1;
            cell.numLabel.font = [UIFont systemFontOfSize:14];

//            cell.numLabel.text = self.numArray[indexPath.row];
            
        }
        return cell;

    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 88, 25)];
        titleLabel.text = @"定制物料";
        [titleView addSubview:titleLabel];
        
        UIButton *ediLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        ediLabel.frame = CGRectMake(SCREEN_WIDTH - 160, 5, 18, 18);
        [ediLabel setImage:[UIImage imageNamed:@"but_modify"] forState:UIControlStateNormal];
        [ediLabel addTarget:self action:@selector(ediAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:ediLabel];
        
        UIButton *ediButton = [UIButton buttonWithType:UIButtonTypeCustom];
        ediButton.frame = CGRectMake(SCREEN_WIDTH - 140, 5, 44, 22);
        
        [ediButton setTitle:@"编辑" forState:UIControlStateNormal];
        [ediButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"1"]) {
            [ediButton setTitle:@"保存" forState:UIControlStateNormal];
            
        }
        [ediButton addTarget:self action:@selector(ediAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:ediButton];
        
        UIButton *addLabel = [UIButton buttonWithType:UIButtonTypeCustom];
        addLabel.frame = CGRectMake(SCREEN_WIDTH - 90, 5, 18, 18);
        [addLabel setImage:[UIImage imageNamed:@"btn_add_wuliao@2x"] forState:UIControlStateNormal];
        
        [addLabel addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:addLabel];
        
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addButton.frame = CGRectMake(SCREEN_WIDTH - 70, 5, 44, 22);
        [addButton setTitle:@"添加" forState:UIControlStateNormal];
        [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:addButton];
        
        
        
        return titleView;
    }
    return nil;
}
//修改面积按钮点击事件
//- (void)handelFixAction:(UIButton *)sender {
//    Second1TableViewCell  * cell = (Second1TableViewCell *)[[sender superview] superview];
//    cell.fixMianJTF.userInteractionEnabled = YES;
//    
//}
-(void)ediAction:(UIButton *)bt{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"ListDiyEdit"] isEqualToString:@"0"]) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"ListDiyEdit"];
        [self.diyTableView reloadData];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"ListDiyEdit"];
        
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"numAdd" object:nil];
        [self.diyTableView reloadData];
    }
    
}
-(void)addAction:(UIButton *)b{
        AddCustomViewController *cus = [[AddCustomViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:cus];
    
        [self presentViewController:naVC animated:YES completion:nil];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 35;
    }
    return 8;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
